//
//  SubjectCellView.m
//  Budget
//
//  Created by Barak on 10/26/12.
//  Copyright (c) 2012 Barak. All rights reserved.
//

#import "SubjectCellView.h"

@implementation SubjectCellView

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        _utilization = 100;
        _totalBudget = 500;
        _limitInput.text = @"200";
        _limitStepper.value = 200;
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* t = [[event allTouches] anyObject];
	CGPoint touchPoint = [t locationInView:self];

    if (self.editing) {
        
        CGPoint aPoint = [t locationInView:_slideArea];
        
        if (aPoint.x > -21 && aPoint.x < _slideArea.frame.size.width) {
            _pointer.frame = CGRectMake(aPoint.x - 21, _pointer.frame.origin.y, _pointer.frame.size.width, _pointer.frame.size.height);
            
            _limit = (aPoint.x / _slideArea.frame.size.width) * _totalBudget;
            _limitStepper.value = _limit;
            _limitInput.text = [NSString stringWithFormat:@"%d", (int)_limit];
        }
    }
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self updateProgress];
}

- (void) setEditing:(BOOL)editing{
    super.editing = editing;
    _editingControls.hidden = !editing;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, editing ? 90 : 51);
    [_pointer setImage:[UIImage imageNamed:(editing ? @"arrowEnabled.png": @"arrow.png")]];

}

- (void) setRule:(BudgetRule *)rule {
    _rule = rule;
    self.utilization = [rule.utilization doubleValue];
    self.limit = [rule.budgetLimit doubleValue];
    self.SubjectLabel.text = rule.category;
    
    _limitInput.text = [NSString stringWithFormat:@"%d", (int)self.limit];
    _limitStepper.value = self.limit;
    
    if ([rule.category isEqualToString:@"Total budget"]) {
        _pointer.hidden = YES;
        _SubjectLabel.font = [UIFont systemFontOfSize:18];
        self.backgroundColor = [UIColor grayColor];
    } else {
        _pointer.hidden = NO;
    }
}

-(void) setUtilization:(double)utilization {
    _utilization = utilization;
    _utilizationLabel.text = [NSString stringWithFormat:@"€ %.2f", utilization];
}


- (void) updateProgress {
    
    _rule.budgetLimit = [NSNumber numberWithInt:_limit];
    
    float utilizationPercent = _utilization / _totalBudget;
    _utilizationProgress.progress = utilizationPercent;
 
    UIColor* tintColor = [UIColor greenColor];
    float percentsFromLimit = _utilization / _limit;
    
    if (percentsFromLimit > 0.75){
        tintColor = [UIColor yellowColor];
    }
    
    if (percentsFromLimit > 1){
        tintColor = [UIColor redColor];
    }

    
    _utilizationProgress.progressTintColor = tintColor;

    
    CGFloat pointerX = _utilizationProgress.frame.origin.x +
                        ((_limit / _totalBudget) * _utilizationProgress.frame.size.width) - 21;
    
    NSLog(@"x=%f", pointerX);
    _pointer.frame = CGRectMake(pointerX, _pointer.frame.origin.y, _pointer.frame.size.width, _pointer.frame.size.height);
    
}

- (IBAction)stepperChange:(id)sender {
    _limitInput.text = [NSString stringWithFormat:@"%d", (int)_limitStepper.value];
    _limit = _limitStepper.value;
    
    [self updateProgress];
}


- (IBAction)textValueChanged:(id)sender {
    
    _limitStepper.value = [_limitInput.text intValue];
    _limit = _limitStepper.value;
    
    [self updateProgress];
    
}
@end
